package it.thegamers.maze.level;

import it.thegamers.maze.interfaces.MapManager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * 30/set/2008
 * 
 * @author Oceans11
 */
public class Level implements MapManager
{
    private BufferedReader input;
    private int[][] matrice;
    private int rowsIndex;
    private int columnsIndex;
    
    public Level(File levelDataFile) throws IOException {
        matrice = new int[MapManager.ROWS][MapManager.COLUMNS];
        rowsIndex = 0;
        columnsIndex = 0;
        input = new BufferedReader(new FileReader(levelDataFile));
        readDataFile();
    }
    
    private void readDataFile() throws IOException {
        String line = "";
        for (int i = 0; i < MapManager.ROWS; i++) {
            line = input.readLine();
            for (int j = 0; j < MapManager.COLUMNS; j++) {
                if (line != null) {
                    matrice[i][j] = Integer.parseInt("" + line.charAt(j));
                }
            }
        }
    }
    
    @Override
    public boolean hasMoreElements() {
        boolean result = columnsIndex < MapManager.COLUMNS;
        if(!result) {
            rowsIndex++;
            columnsIndex = 0;
        }
        return result;
    }
    
    @Override
    public boolean hasMoreRows() {
        return rowsIndex < MapManager.ROWS;
    }
    
    @Override
    public int nextElement() {
        return matrice[rowsIndex][columnsIndex++];
    }
    
    @Override
    public void reset() {
        matrice = null;
    }
}